##################################################################
# File : /lua/ui/splash/splash.lua
# Description : Create and control pre-game splash screens
# Author : GPG / Neruz
##################################################################

local UIMain = import('/lua/ui/uimain.lua')
local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Movie = import('/lua/maui/movie.lua').Movie
local Group = import('/lua/maui/group.lua').Group
local LazyVar = import('/lua/lazyvar.lua')

local splashScreen = nil

SplashMovie = Class(Group) {

    __init = function(self, parent, filename)
        Group.__init(self, parent)
        self.Movie = Movie(self, filename)
        LayoutHelpers.AtCenterIn(self.Movie, self)

        local scale = LazyVar.Create()
        scale:Set(function () return math.min(self.Width() / self.Movie.MovieWidth(), self.Height() / self.Movie.MovieHeight()) end)
        self.Movie.Width:Set(function () return self.Movie.MovieWidth() * scale() end)
        self.Movie.Height:Set(function () return self.Movie.MovieHeight() * scale() end)

        self.Movie.OnFinished = function(movie) self:OnFinished() end
    end,

    Play = function(self)
        self.Movie:Play()
    end,

    HandleEvent = function(self, event)
        if event.Type == 'ButtonPress' or event.Type == 'ButtonDClick' then
            self:OnFinished()
            return true
        else
            return false
        end
    end,
}

function CreateUI()
	EngineStartFrontEndUI()
#     local frame = GetFrame(0)

#     if HasCommandLineArg('/demo') then
#         EngineStartFrontEndUI()
#         return
#     end

#     splashScreen = SplashMovie(frame, '/movies/dg_intro.bik')

#     splashScreen.EscapeHandler = function()
#         if splashScreen then
#             splashScreen:OnFinished()
#         end
#     end

#     splashScreen.OnFinished = function(self)
#         self:Destroy()
#         UIMain.RemoveEscapeHandler(splashScreen.EscapeHandler)
#         EngineStartFrontEndUI()
#     end
#     LayoutHelpers.FillParent(splashScreen, frame)
#     splashScreen:Play()

#     UIMain.AddEscapeHandler(splashScreen.EscapeHandler)
end
